//package com.yang.lock.core;
//
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import com.yang.lock.annotation.YangLock;
//import com.yang.lock.config.LockConfig;
//import com.yang.lock.model.LockInfo;
//import com.yang.lock.model.LockType;
//
///**
// * <p>
// * 锁信息提供程序
// * </p>
// * @author yang
// * @date 2023/7/13
// */
//public class LockInfoProvider {
//
//    private static final String LOCK_NAME_PREFIX = "lock";
//    private static final String LOCK_NAME_SEPARATOR = ".";
//
//
//    @Autowired
//    private LockConfig lockConfig;
//
//    @Autowired
//    private BusinessKeyProvider businessKeyProvider;
//
//    private static final Logger logger = LoggerFactory.getLogger(LockInfoProvider.class);
//
//    LockInfo get(JoinPoint joinPoint, YangLock yangLock) {
//        // 获取方法信息
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        // 获取选择锁的类型
//        LockType type = yangLock.lockType();
//        // 获取自定的锁keyName
//        String businessKeyName = businessKeyProvider.getKeyName(joinPoint, yangLock);
//        // 锁的名字，锁的粒度就是这里控制的
//        String lockName = LOCK_NAME_PREFIX + LOCK_NAME_SEPARATOR + getName(yangLock.name(), signature) + businessKeyName;
//        // 获取锁失败的等待时间
//        long waitTime = getWaitTime(yangLock);
//        // 锁超时释放时间
//        long leaseTime = getLeaseTime(yangLock);
//        // 如果占用锁的时间设计不合理，则打印相应的警告提示
//        if(leaseTime == -1 && logger.isWarnEnabled()) {
//            logger.warn("Trying to acquire Lock({}) with no expiration, " +
//                        "lock will keep prolong the lock expiration while the lock is still holding by current thread. " +
//                        "This may cause dead lock in some circumstances.", lockName);
//        }
//        // 创建并返回锁信息
//        return new LockInfo(type,lockName,waitTime,leaseTime);
//    }
//
//    /**
//     * 获取锁的name，如果没有指定，则按全类名拼接方法名处理
//     */
//    private String getName(String annotationName, MethodSignature signature) {
//
//        if (annotationName.isEmpty()) {
//            return String.format("%s.%s", signature.getDeclaringTypeName(), signature.getMethod().getName());
//        } else {
//            return annotationName;
//        }
//    }
//
//
//    private long getWaitTime(YangLock lock) {
//        return lock.waitTime() == Long.MIN_VALUE ?
//                lockConfig.getWaitTime() : lock.waitTime();
//    }
//
//    private long getLeaseTime(YangLock lock) {
//        return lock.leaseTime() == Long.MIN_VALUE ?
//                lockConfig.getLeaseTime() : lock.leaseTime();
//    }
//}
